/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uber.netbsd.community.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * This code was taken from stripes examples and was hard modified
 * Represents a person (user)
 *
 * @author Tim Fennell
 * @author alex
 */
@PersistenceCapable
public class Person extends Struct implements Serializable, UserDetails{

    /**
     * first name
     */
    @Persistent
    private String firstName;

    /**
     * last name
     */
    @Persistent
    private String lastName;

    /**
     * user's email address
     */
    @Persistent
    private String email;
    /**
     * user's password
     */
    @Persistent
    private String password;

    /**
     * list of user's roles
     */
    @Persistent
    private List<Role> roles = new ArrayList<Role>();
    
    /**
     * if this user account is expired
     */
    @Persistent
    private boolean accountExpired;
   
    @Persistent
    private boolean credentialsExpired;


     public void fillFrom(Person external) {

         super.fillFrom(external);

       //  if (external.getFirstName()!=null)
            setFirstName(external.getFirstName());

        // if (external.getLastName()!=null)
         setLastName(external.getLastName());

         //if (external.getEmail()!=null)
         setEmail(external.getEmail());

         if (external.getPassword()!=null)
         setPassword(external.getPassword());

         roles.clear();

         for (Role r:external.getRoles())
             roles.add(r);
     }

   
    /** Gets the username of the person. */
    @Override
    public String getUsername() {
        return name;
    }

    /** Sets the username of the user. */
    public void setUsername(String username) {
        this.name = username;
    }

    /** Gets the first name of the person. */
    public String getFirstName() {
        return firstName;
    }

    /** Sets the first name of the user. */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /** Gets the last name of the person. */
    public String getLastName() {
        return lastName;
    }

    /** Sets the last name of the user. */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /** Gets the person's email address. */
    public String getEmail() {
        return email;
    }

    /** Sets the person's email address. */
    public void setEmail(String email) {
        this.email = email;
    }

    /** Gets the person's unencrypted password. */
    @Override
    public String getPassword() {
        return password;
    }

    /** Sets the person's unencrypted password. */
    public void setPassword(String password) {
        this.password = password;
    }

    public List<Role> getRoles() {
        
    return roles;
    }

    public List<Role> getAllRoles() {

        List<Role> out = new ArrayList<Role>();
        out.addAll(Arrays.asList(Role.values()));

        return out;
    }
   
     public void addRole(Role role) {
      
       roles.add(role);
    }


    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> out =  new ArrayList<GrantedAuthority>();
        if (roles==null)
            return out;

        for (Role r:roles)
             out.add(r);
        
        return out;
    }


    @Override
    public boolean isEnabled() {
        return !disabled;
    }

    public boolean isAccountExpired() {
        return accountExpired;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonExpired()
     */

    @Override
    public boolean isAccountNonExpired() {
        return !isAccountExpired();
    }


    public boolean isAccountLocked() {
        return disabled;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonLocked()
     */

    @Override
    public boolean isAccountNonLocked() {
        return !isAccountLocked();
    }


    public boolean isCredentialsExpired() {
        return credentialsExpired;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isCredentialsNonExpired()
     */

    @Override
    public boolean isCredentialsNonExpired() {
        return !credentialsExpired;
    }

    /** Equality is determined to be when the ID numbers match. */
   

    @Override
    public String toString() {

        return "type:PERSON,"+super.toString();
    }
}
